package com.xcity.game.assist;

import java.text.MessageFormat;
import java.util.Map;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Employee;
import com.xcity.db.entity.Player;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.employee.EmployeeQuality;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.employee.template.EmployeeProduceMoneyByQualityV2Template;
import com.xcity.game.mail.MailAttach;
import com.xcity.game.mail.MailService;

import naga.x.App;

public class AssistBuild {
	@JSONField(name = "0")
	protected long ownerId;//原主人ID(被协助者)
	@JSONField(name = "1")
	protected int id;//建筑ID
	@JSONField(name = "2")
	protected long[] employees;
	@JSONField(name = "3")
	protected long lastStartMakeMoneyTime;//上次开始产钱时间
	@JSONField(name = "4")
	protected long assistStartTime;//开始援助时间

//	@JSONField(serialize = false)
//	protected AssistManager manager;
	
	public static int ASSISTANT_TAKE_MONEY_PERCENT = 0;//5;//被邀请协助方会额外获得X%现金产出
	
	public AssistBuild(){
	}
	
	public AssistBuild(AssistManager manager, long ownerId, int buildid, long[] employees, long now){
		this.ownerId = ownerId;
		this.id = buildid;
//		this.manager = manager;
		this.employees = employees;
		this.lastStartMakeMoneyTime = now;
		this.assistStartTime = now;
	}

	public long getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(long ownerId) {
		this.ownerId = ownerId;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public long[] getEmployees() {
		return employees;
	}

	public void setEmployees(long[] employees) {
		this.employees = employees;
	}

	public long getLastStartMakeMoneyTime() {
		return lastStartMakeMoneyTime;
	}

	public void setLastStartMakeMoneyTime(long lastStartMakeMoneyTime) {
		this.lastStartMakeMoneyTime = lastStartMakeMoneyTime;
	}
	
	public long getAssistStartTime() {
		return assistStartTime;
	}

	public void setAssistStartTime(long assistStartTime) {
		this.assistStartTime = assistStartTime;
	}

//	public AssistManager getManager() {
//		return manager;
//	}
//
//	public void setManager(AssistManager manager) {
//		this.manager = manager;
//	}
	/**
	 * 计算钱
	 * @param 当前时间
	 * @param buildFactor 建筑加成百分点  100:100%
	 * @return {原主人获得金钱, 协助者获得金钱}
	 */
	public int[] calcMoney(Player assistant, long timeNow, int buildFactor){
		int[] ret = new int[]{0, 0};
		if(lastStartMakeMoneyTime <= 0){
			return ret;
		}
		if(assistStartTime <= 0){
			return ret;
		}
		long endTime = timeNow;
		if(endTime > assistStartTime + BuildV2.BUILD_RETURN_MILLISECOND){
			endTime = assistStartTime + BuildV2.BUILD_RETURN_MILLISECOND;
		}
		long elapseMS = endTime - this.lastStartMakeMoneyTime;
		if(elapseMS < 0){
			return ret;
		}
		long max_millisecond = BuildV2.MAX_TAKE_MONEY_SECOND * 1000;
		if(elapseMS > max_millisecond){
			elapseMS = max_millisecond;
		}
		
		Map<EmployeeQuality, EmployeeProduceMoneyByQualityV2Template> quality2Temp = App.getApp().getServiceManager().get(EmployeeService.class)
				.configure().getCommonConfigure().getQuality2produceMoneyTemplate();
		int addMoneyPerMinute = 0;
		long[] eids = employees;
		for(int i = 0; i < eids.length; i++){
			Employee e = assistant.getEmployees().get(eids[i]);
			if(e != null){
				EmployeeQuality quality = e.getTemplate().getQuality();
				EmployeeProduceMoneyByQualityV2Template temp = quality2Temp.get(quality);
				if (temp != null) {
					addMoneyPerMinute = addMoneyPerMinute + temp.getProduceMoney();
				}
			}
		}
//		int ownerMoney = (int)(elapseMS / 1000L * addMoneyPerMinute * buildFactor / 100L / 60L);
		int ownerMoney = (int)(elapseMS / 60L / 1000L * addMoneyPerMinute * buildFactor / 100L);
		int assistMoney = (int)(ownerMoney * ASSISTANT_TAKE_MONEY_PERCENT / 100);
		ret[0] = ownerMoney;//主人获得金钱
		ret[1] = assistMoney;//协助者获得金钱
		return ret;
	}
	
	/**
	 * 领钱
	 * @param needMail TODO
	 */
	public int[] takeMoney(Player assistant, long timeNow, BuildV2 build, boolean needMail){
		int[] ret = calcMoney(assistant, timeNow, build.getTemplate().getProduceMoneyFactor());
		if(ret[0] > 0){
			if(needMail){
				MailAttach attach = new MailAttach(ret[0], 0);
				String title = MessageConst.ASSIST_BUILD_TAKE_MONEY;
				String content = MessageFormat.format(MessageConst.ASSIST_BUILD_TAKE_MONEY_CONTENT, build.getTemplate().getName(), ret[0]);
				App.getApp().getServiceManager().get(MailService.class).sendSystemMail(build.getVm().getOwner().getId(), title, content, attach);
			}else{
				build.getVm().getOwner().addGold(ret[0], Cause.ASS_GET_MONEY);
			}
			this.lastStartMakeMoneyTime = timeNow;
		}
		if(ret[1] > 0){
			MailAttach attach = new MailAttach(ret[1], 0);
			String title = MessageConst.ASSIST_BUILD_TAKE_MONEY_ASS;
			String content = MessageFormat.format(MessageConst.ASSIST_BUILD_TAKE_MONEY_CONTENT_ASS, build.getVm().getOwner().getName(), ret[1]);
			App.getApp().getServiceManager().get(MailService.class).sendSystemMail(this.getId(), title, content, attach);
		}
		return ret;
	}
}
